<template>
  <a-modal
    title="同步目标设置"
    :width="1000"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-card :bordered="true">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="false"
        :rowKey="(record) => record.id"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('carIntegralInfo:add')" >
          <a-button type="primary" v-if="hasPerm('carIntegralInfo:add')" icon="plus" @click="$refs.addForm.add(appUserId)">积分充值</a-button>
        </template>
        <span slot="type" slot-scope="text">
          {{ typeFilter(text) }}
        </span>
        <!-- <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('carIntegralInfo:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('carIntegralInfo:edit') & hasPerm('carIntegralInfo:delete')"/>
          <a-popconfirm v-if="hasPerm('carIntegralInfo:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span> -->
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </a-modal>
</template>
<script>
  import { STable, XDown, XCard } from '@/components'
  import { carIntegralInfoPage, carIntegralInfoDelete, carIntegralInfoExport } from '@/api/modular/main/carintegralinfo/carIntegralInfoManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      STable,
      XDown,
      addForm,
      editForm,
      XCard
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        visible: false,
        appUserId: [],
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '用户编号',
            align: 'center',
            dataIndex: 'userId'
          },
          {
            title: '积分数量',
            align: 'center',
            dataIndex: 'integralNum'
          },
          {
            title: '消费类型',
            align: 'center',
            scopedSlots: { customRender: 'type' }
          },
          {
            title: '管理订单id',
            align: 'center',
            dataIndex: 'orderId'
          },
          // {
          //   title: '0未删除 1已删除',
          //   align: 'center',
          //   dataIndex: 'deleteFlag'
          // },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return carIntegralInfoPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
      }
    },
    created () {
      // if (this.hasPerm('carIntegralInfo:edit') || this.hasPerm('carIntegralInfo:delete')) {
      //   this.columns.push({
      //     title: '操作',
      //     width: '150px',
      //     dataIndex: 'action',
      //     scopedSlots: { customRender: 'action' }
      //   })
      // }
    },
    methods: {
      // 打开此页面首先加载此方法
      index (record) {
        this.visible = true
        this.appUserId = record.id
        this.queryParam.userId = record.id
        try {
          this.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      },
      /**
       * 设置消费类型
       */
      typeFilter (obj) {
        if (obj === 1) {
          return '充值'
        }
        return '消费'
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.carIntegralInfoDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.carIntegralInfoDelete(paramIds)
      },
      carIntegralInfoDelete (record) {
        carIntegralInfoDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        carIntegralInfoExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
